package com.bnd.common.holder;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
public class VoiceIdHolder {
    public static final String apiKey = System.getenv("DASHSCOPE_API_KEY");
    private static  final String prefix = "custom";
    private static final List<String> voiceIdList = Collections.synchronizedList(new ArrayList<>(Arrays.asList(
            "longhuhu", "longanpei", "longdaiyu", "longgaoseng", "longyingmu", "longyingxun", "longyingcui", "longyingda",
            "longyingjing", "longyingyan", "longyingtian", "longyingbing", "longyingtao", "longyingling", "longyumi_v2",
            "longxiaochun_v2", "longxiaoxia_v2", "longanran", "longanxuan", "longanchong", "longanping", "longbaizhi", "longsanshu",
            "longxiu_v2", "longmiao_v2", "longyue_v2", "longnan_v2", "longyuan_v2", "longanrou", "longqiang_v2", "longhan_v2", "longxing_v2",
            "longhua_v2", "longwan_v2", "longcheng_v2", "longfeifei_v2", "longxiaocheng_v2", "longzhe_v2", "longyan_v2", "longtian_v2", "longze_v2",
            "longshao_v2", "longhao_v2", "kabuleshen_v2", "longjielidou_v2", "longling_v2", "longke_v2", "longxian_v2", "longlaotie_v2", "longjiayi_v2",
            "longtao_v2", "longfei_v2", "libai_v2", "longjin_v2", "longshu_v2", "loongbella_v2", "longshuo_v2", "longxiaobai_v2", "longjing_v2",
            "loongstella_v2", "loongeva_v2", "loongbrian_v2", "loongluna_v2", "loongluca_v2", "loongemily_v2", "loongeric_v2", "loongabby_v2",
            "loongannie_v2", "loongandy_v2", "loongava_v2", "loongbeth_v2", "loongbetty_v2", "loongcindy_v2", "loongcally_v2", "loongdavid_v2",
            "loongdonna_v2", "loongkyong_v2", "loongtomoka_v2", "loongtomoya_v2"
    )));

    static {
        if (StrUtil.isNotBlank(apiKey)){
            VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
            // 查询音色
            try {
                Voice[] voices = service.listVoice(prefix, 0, 100);
                List<String> voiceIdList = Arrays.stream(voices).map(Voice::getVoiceId).toList();
                log.info("[音色ID] 获取成功, 共{}个, 音色ID列表: {}", voiceIdList.size(), voiceIdList);
                VoiceIdHolder.voiceIdList.addAll(voiceIdList);
            } catch (NoApiKeyException e) {
                log.warn("[音色ID] api-key获取异常: {}", e.getMessage());
            } catch (InputRequiredException e) {
                log.warn("[音色ID] 参数获取异常: {}", e.getMessage());
            }
        }
    }

    /**
     * 校验音色是否存在
     * @param voiceId 音色ID
     * @return 是否存在
     */
    public static boolean isValidVoiceId(String voiceId) {
        if (StrUtil.isBlank(voiceId)) {
            return false;
        }
        return voiceIdList.contains(voiceId);
    }

    /**
     * 获取所有音色ID列表
     * @return 音色ID列表
     */
    public static List<String> getAllVoiceIds() {
        return new ArrayList<>(voiceIdList);
    }

    /**
     * 添加音色ID
     * @param voiceId 音色ID
     * @return 是否添加成功
     */
    public static boolean addVoiceId(String voiceId) {
        if (StrUtil.isBlank(voiceId)) {
            return false;
        }
        if (!voiceIdList.contains(voiceId)) {
            return voiceIdList.add(voiceId);
        }
        return false;
    }

    /**
     * 批量添加音色ID
     * @param voiceIds 音色ID列表
     * @return 添加成功的数量
     */
    public static int addVoiceIds(List<String> voiceIds) {
        if (voiceIds == null || voiceIds.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (String voiceId : voiceIds) {
            if (addVoiceId(voiceId)) {
                count++;
            }
        }
        return count;
    }
}
